package com.gzhryc.shared_device.oem.h5.mng.controllers.cdcz;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZOrder;
import com.gzhryc.shared_device.oem.code.order.RedisCacheManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;

public class ChargeSocketOrderWorkingController {

    @Route(value = "/cdcz/order/working/list.html")
    public String list(HttpServletRequest request, HttpSession session) {
        return "/cdcz/order/working/list.html";
    }

    @Route(value = "/cdcz/order/working/list.data", method = "post")
    public ListResult<CDCZOrder> listData(HttpServletRequest request, HttpSession session) throws ParseException {
        ListResult<CDCZOrder> result = new ListResult<>(DtoConstants.NULL_DATA,"暂无数据");
        List<CDCZOrder> data = RedisCacheManager.findWorkingCDCZDeviceOrder();
        if(data != null && data.size() > 0) {
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(data);
        }
        return result;
    }

    @Route(value = { "/cdcz/order/working/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败");
        String tradeNo = WebUtils.getString(request, "tradeNo");
        if (StringTools.isNotBlank(tradeNo)) {
            try {
                RedisCacheManager.removeWorkingCDCZDeviceOrder(tradeNo);
                result.setCode(DtoConstants.SUCCESS, null);
            }catch (Exception e){
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }
}
